package com.group.controller;

import com.base.view.GeneralResult;
import com.base.view.ListResult;
import com.group.model.ClockinChallenge;
import com.group.model.User;
import com.group.model.UserTicker;
import com.group.util.NoteException;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 打卡挑战
 */
@Controller
public class ClockinController extends BaseController {

    /**
     * 首页 瓜分金额
     *
     * @param request HttpServletRequest
     * @return Double
     */
    @GetMapping(value = "/clockin/queryCarveUpTotal")
    @ResponseBody
    public GeneralResult<Double> queryCarveUpTotal(HttpServletRequest request) {

        getUser(request);

        if (clokinService.judgeTimeScope("战况")) {
            //今日
            return new GeneralResult<Double>().setValue(clokinDao.queryCarveUpWecc(df.format(new Date())));
        }
        //昨日
        return new GeneralResult<Double>().setValue(clokinDao.queryCarveUpWecc(clokinService.getDayDate(-1)));
    }

    /**
     * 首页 当前参加人数
     *
     * @param request HttpServletRequest
     * @return Integer
     */
    @GetMapping(value = "/clockin/queryJoinPeoples")
    @ResponseBody
    public GeneralResult<Integer> queryJoinPeoples(HttpServletRequest request) {

        getUser(request);

        if (clokinService.judgeTimeScope("战况")) {
            return new GeneralResult<Integer>().setValue(clokinDao.queryJoinPeople(df.format(new Date())));
        }
        return new GeneralResult<Integer>().setValue(clokinDao.queryJoinPeople(clokinService.getDayDate(-1)));
    }

    /**
     * 首页 头像列表
     *
     * @param request HttpServletRequest
     * @param start 开始
     * @param limit 条数
     * @return
     */
    @GetMapping(value = "/clockin/queryHeadImages")
    @ResponseBody
    public ListResult<String> queryHeadImages(HttpServletRequest request, Integer start, Integer limit) {

        getUser(request);

        if (start == null || limit == null) {
            throw new NoteException("start或limit不能为空");
        }

        if (clokinService.judgeTimeScope("战况")) {
            return new ListResult<String>().setList(clokinDao.queryUserHeadimages(df.format(new Date()), start, limit)).setTotal(0);
        }
        return new ListResult<String>().setList(clokinDao.queryUserHeadimages(clokinService.getDayDate(-1), start, limit)).setTotal(0);
    }

    /**
     * 首页 战况的 成功/失败总数
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/clockin/queryTotal")
    @ResponseBody
    public GeneralResult<Integer> queryTotal(HttpServletRequest request, String status) {

       getUser(request);

        if (status == null) {
            throw new NoteException("status不能为空");
        }

        if (clokinService.judgeTimeScope("战况")) {
            return new GeneralResult<Integer>().setValue(clokinDao.queryStatusNum(status, df.format(new Date())));
        }
        return new GeneralResult<Integer>().setValue(clokinDao.queryStatusNum(status, clokinService.getDayDate(-1)));
    }

    /**
     * 首页 早起之星
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/clockin/queryEarlyStar")
    @ResponseBody
    public GeneralResult<String> queryEarlyStar(HttpServletRequest request) {

        getUser(request);

        if (clokinService.judgeTimeScope("战况")) {
            return new GeneralResult<String>().setValue(clokinDao.queryEarlyStar(df.format(new Date())));
        }
        return new GeneralResult<String>().setValue(clokinDao.queryEarlyStar(clokinService.getDayDate(-1)));
    }

    /**
     * 首页 手气之星
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/clockin/queryLuckStar")
    @ResponseBody
    public GeneralResult<Double> queryLuckStar(HttpServletRequest request) {

        getUser(request);

        if (clokinService.judgeTimeScope("战况")) {
            return new GeneralResult<Double>().setValue(clokinDao.queryLuckStar(df.format(new Date())));
        }
        return new GeneralResult<Double>().setValue(clokinDao.queryLuckStar(clokinService.getDayDate(-1)));
    }


    /**
     * 首页 毅力之星
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/clockin/queryWillpowerStar")
    @ResponseBody
    public GeneralResult<Integer> queryWillpowerStar(HttpServletRequest request) {

        getUser(request);

        return new GeneralResult<Integer>().setValue(clokinDao.queryWillpowerStar());

    }

    /**
     * 首页 用户状态 ：未报名/等待打卡/早起成功/早起失败
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/clockin/queryUserStatus")
    @ResponseBody
    public GeneralResult<String> queryUserStatus(HttpServletRequest request) {

        User user = getUser(request);

        ClockinChallenge clockin = new ClockinChallenge();

        if (clokinService.judgeTimeScope("战况")) {

            clockin = clokinDao.queryIsJoin(user.getId(), df.format(new Date()));
        } else {
            clockin = clokinDao.queryIsJoin(user.getId(), clokinService.getDayDate(-1));
        }

        if (clockin == null) {
            return new GeneralResult<String>().setValue("未报名");
        }

        return new GeneralResult<String>().setValue(clockin.getStatus());
    }

    /**
     * 参与报名
     *
     * @param putWecc 投入 wecc 金额
     * @return
     */
    @GetMapping(value = "/clockin/join")
    @ResponseBody
    @Transactional
    public GeneralResult<String> join(HttpServletRequest request, Double putWecc) {

        User user = getUser(request);

        if (putWecc == null) {
            throw new NoteException("putWecc不能为空");
        }

        if (putWecc <= 0) {
            throw new NoteException("putWecc必须大于0");
        }

        if (clokinService.judgeTimeScope("睡眠")) {
            throw new NoteException("现在是睡眠时间不能报名");
        }

        Long userId = user.getId();

        if (clokinDao.queryIsJoin(userId, df.format(new Date())) != null) {
            throw new NoteException("已参加过报名");
        }

        UserTicker ut = tickerService.getUserTicker(userId, "wecc");

        double num = ut.getNum() - putWecc;
        if (num < 0.0) {
            throw new NoteException("wecc 余额不足");
        }

        tickerService.addTicker(userId, "wecc", -putWecc, "早起打卡报名转出");

        ClockinChallenge clockin = new ClockinChallenge();
        clockin.setUserId(userId);
        clockin.setUserName(user.getName());
        clockin.setUserHeadimage(user.getHeadimage());
        clockin.setPutWecc(putWecc);
        clockin.setJoinTime(df.format(new Date()));
        clockin.setStatus("等待打卡");
        beanDao.save(clockin);

        return new GeneralResult<String>().setValue("报名成功");
    }

    /**
     * 打卡操作：昨天报名的，今天打卡
     *
     * @return
     */
    @GetMapping(value = "/clockin/challenge")
    @ResponseBody
    @Transactional
    public GeneralResult<String> challenge(HttpServletRequest request) {
        User user = getUser(request);

        if (!clokinService.judgeTimeScope("打卡")) {
            throw new NoteException("请在06:00-08:00时间内打卡");
        }

        ClockinChallenge clockin = clokinDao.queryIsJoin(user.getId(), clokinService.getDayDate(-1));

        if (clockin == null) {
            throw new NoteException("请先报名，明天来打卡");
        }

        clockin.setStatus("早起成功");
        clockin.setClockInTime(new Date());
        beanDao.save(clockin);

        return new GeneralResult<String>().setValue("打卡成功");
    }

    /**
     * 我的战绩 总投入
     *
     * @return
     */
    @GetMapping("/clockin/queryMyPutWeccTotal")
    @ResponseBody
    public GeneralResult<Double> queryMyPutWeccTotal(HttpServletRequest request) {

        User user = getUser(request);

        return new GeneralResult<Double>().setValue(clokinDao.queryMyPutWeccSum(user.getId()));
    }

    /**
     * 我的战绩 总收入
     *
     * @return
     */
    @GetMapping("/clockin/queryMyIncomeWeccTotal")
    @ResponseBody
    public GeneralResult<Double> queryMyIncomeWeccTotal(HttpServletRequest request) {

        User user = getUser(request);

        return new GeneralResult<Double>().setValue(clokinDao.queryMyIncomeWeccSum(user.getId()));
    }

    /**
     * 我的战绩 参与数
     *
     * @return
     */
    @GetMapping("/clockin/queryMyJoin")
    @ResponseBody
    public GeneralResult<Integer> queryMyJoin(HttpServletRequest request) {

        User user = getUser(request);

        return new GeneralResult<Integer>().setValue(clokinDao.queryMyJoinSum(user.getId()));
    }

    /**
     * 我的战绩 成功数
     *
     * @return
     */
    @GetMapping("/clockin/queryMySuccess")
    @ResponseBody
    public GeneralResult<Integer> queryMySuccess(HttpServletRequest request) {

        User user = getUser(request);

        return new GeneralResult<Integer>().setValue(clokinDao.queryMySuccessSum(user.getId()));
    }

    /**
     * 我的战绩 每日明细
     *
     * @param request
     * @param start
     * @param limit
     * @return
     */
    @GetMapping("/clockin/queryMyEveryDetail")
    @ResponseBody
    public ListResult<ClockinChallenge> queryMyEveryDetail(HttpServletRequest request, Integer start, Integer limit) {
        User user = getUser(request);

        if (start == null || limit == null) {
            throw new NoteException("start或limit不能为空");
        }

        return new ListResult<ClockinChallenge>().setList(clokinDao.queryMyEveryDetail(user.getId(), start, limit)).setTotal(0);
    }

}
